<?php
require 'config.php';
$get_num = $_REQUEST['get_num'];
$yiji_id = $_REQUEST['yiji_id'];
$erji_id = $_REQUEST['erji_id'];
$shop_id = $_REQUEST['shop_id'];
if ($get_num == 1) {
    $sql = "select goodsId,goodsImg,goodsName,shopPrice,goodsStock,brandId,goodsCatId1,goodsCatId2,goodsCatId3,shopCatId1,shopCatId2,goodsThums,shopId from `" . $oto . "_goods` where `shopCatId1`='$yiji_id' and `isSale`=1 and shopId='{$shop_id}' and goodsFlag='1' and goodsStock>0 and goodsStatus='1'";
} else if ($get_num == 2) {
    $sql = "select goodsId,goodsImg,goodsName,shopPrice,goodsStock,brandId,goodsCatId1,goodsCatId2,goodsCatId3,shopCatId1,shopCatId2,goodsThums,shopId from `" . $oto . "_goods` where `shopCatId1`='$yiji_id' and `shopCatId2`='$erji_id' and `isSale`=1 and shopId='{$shop_id}' and goodsFlag='1' and goodsStock>0 and goodsStatus='1'";
}
$result = $db->query($sql);
while ($row = $result->fetch_assoc()) {
    $goods_list[] = $row; //将取得的所有数据赋值给person_info数组
}
if (isset($goods_list)) {
    $time_star = time();
    $time_end = $time_star - 3600 * 24 * 30;
    $time_star1 = date('Y-m-d H:i:s', $time_end);
    $time_end1 = date('Y-m-d H:i:s', $time_star);
    for ($i = 0; $i < count($goods_list); $i++) {
        $goods_id = $goods_list[$i]['goodsId'];
        $sql2 = "select * from `" . $oto . "_goods_attributes`,`" . $oto . "_attributes` where " . $oto . "_goods_attributes.goodsId='$goods_id' and isPriceAttr=1 and " . $oto . "_goods_attributes.attrId=" . $oto . "_attributes.attrId order by " . $oto . "_attributes.attrId ASC";
        $result = $db->query($sql2);
        //file_put_contents("tsxx.txt", "\r\n".$goods_list[$i]['goodsName'].":".$sql2."\r\n", FILE_APPEND);
        while ($row = $result->fetch_assoc()) {
            $shuxing_info[$i] = $row; //将取得的所有数据赋值给person_info数组
        }
        if (!isset($shuxing_info[$i])) {
            $goods_list[$i]['is_shuxing'] = 0;
        } else {
            $goods_list[$i]['is_shuxing'] = 1;
        }
        $sql = "select * from `" . $oto . "_orders`,`" . $oto . "_order_goods` where " . $oto . "_orders.orderId=" . $oto . "_order_goods.orderId and " . $oto . "_order_goods.goodsId='{$goods_id}' and createTime>='$time_star1' and createTime<='$time_end1' and orderStatus='4'";
        $result = $db->query($sql);
        while ($row = $result->fetch_assoc()) {
            $order_list[$i][] = $row; //将取得的所有数据赋值给person_info数组
        }
        $sum = 0;
        if (isset($order_list[$i])) {
            for ($j = 0; $j < count($order_list[$i]); $j++) {
                $sum += $order_list[$i][$j]['goodsNums'];
            }
        }
        $goods_list[$i]['yue_xiaoliang'] = $sum;
        $sum = 0;
        $sql = "select * from " . $oto . "_goods_appraises where goodsId='{$goods_id}' and isDel='0' ";
        $result = $db->query($sql);
        while ($row = $result->fetch_assoc()) {
            $appraises[$i][] = $row; //将取得的所有数据赋值给person_info数组
        }
        if (isset($appraises[$i])) {
            $sum = count($appraises[$i]);
        }
        $goods_list[$i]['goods_appraises'] = $sum;
        //file_put_contents("tsxx.txt", "\r\n"."是否有属性:".$goods_list[$i]['is_shuxing']."\r\n", FILE_APPEND);
    }
    echo json_encode($goods_list);
} else {
    echo '[]';
}
